/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.crab.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.web.BaseController;
import com.kaaction.modules.crab.entity.CrabArea;
import com.kaaction.modules.crab.entity.Order;
import com.kaaction.modules.crab.service.CouponDetailService;
import com.kaaction.modules.crab.service.CrabAreaService;
import com.kaaction.modules.crab.service.CrabProductService;
import com.kaaction.modules.crab.service.OrderService;

/**
 * 订单管理Controller
 * @author huanghaiming
 * @version 2017-07-21
 */
@Controller
@RequestMapping(value = "${adminPath}/crab/orderCreate")
public class OrderCreateController extends BaseController {

	@Autowired
	private OrderService orderService;
	
	@Autowired
	private CrabAreaService crabAreaService;
	
	@Autowired
	CouponDetailService couponDetailService;
	
	@Autowired
	CrabProductService crabProductService;
	
	@ModelAttribute
	public Order get(Order order) {
		order.setHopeDate(new Date());
		order.setListProvince(crabAreaService.selectListProvince());
		if(order.getListProvince().size()>0){
			String province = order.getListProvince().get(0).getCode();
			if(StringUtils.isNotBlank(province)){
				order.setListCity(crabAreaService.selectListCity(province));
				String city = order.getListCity().get(0).getCode();
				if(StringUtils.isNotBlank(city)){
					order.setListCounty(crabAreaService.selectListCounty(city));
				}else{
					order.setListCounty(new ArrayList<CrabArea>());
				}
			}
		}
		
		return order;
	}
	
	@RequiresPermissions("crab:orderCreate:view")
	@RequestMapping(value = {"list", ""})
	public String form(Order order, Model model) {
		model.addAttribute("order", order);
		return "modules/crab/orderCreateForm";
	}

	@RequiresPermissions("crab:orderCreate:save")
	@RequestMapping(value = "save")
	public String save(Order order, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, order)){
			return form(order, model);
		}
		
		String cardNum = order.getCardNum();
		String password = order.getPassword();
		
		int cnt = orderService.selEqual(cardNum,password);
		if(cnt==0){	
			Map<String,Object>  map = couponDetailService.selCoupon(cardNum,"");
			
			if(map!=null&&map.size()>0){
				String pw = map.get("password").toString();
				if(!password.equals(pw)){
					model.addAttribute("message", "密码不正确");
					return form(order, model);
				}
			}else{
				model.addAttribute("message", "兑换券卡号不正确或未激活");
				return form(order, model);
			}
			orderService.insert(order);
			order.setCardNum("");
			order.setPassword(null);
			order.setName("");
			order.setMobile("");
			order.setAddress("");
			order.setRemarks("");
			addMessage(redirectAttributes, "电话提货成功");
		}else{
			String province = order.getProvince();
			if(StringUtils.isNotBlank(province)){
				order.setListCity(crabAreaService.selectListCity(province));
			}
			String city = order.getCity();
			if(StringUtils.isNotBlank(city)){
				order.setListCounty(crabAreaService.selectListCounty(city));
			}
			model.addAttribute("message", "该卡号的订单已存在");
			return form(order, model);
		}
		
		return "redirect:"+Global.getAdminPath()+"/crab/orderCreate/?repage";
	}
	
	@RequiresPermissions("crab:orderCreate:address")
	@RequestMapping(value = "getCitys")
	public String getCitys(Order order, Model model, RedirectAttributes redirectAttributes) {
		String province = order.getProvince();
		if(StringUtils.isNotBlank(province)){
			order.setListCity(crabAreaService.selectListCity(province));
			
			if(order.getListCity()!=null&&order.getListCity().size()>0){
				String city = order.getListCity().get(0).getCode();
				if(StringUtils.isNotBlank(city)){
					order.setListCounty(crabAreaService.selectListCounty(city));
				}
			}else{
				order.setListCounty(new ArrayList<CrabArea>());
			}
			
		}
		String password  = order.getPassword();
		String pw = "";
		if(password!=null){
			pw = password.toString();
		}
		
		Map<String,Object> map =  couponDetailService.selCoupon(order.getCardNum(),pw);
		
		if(map!=null&&map.size()>0){
			String couponId = (String) map.get("couponId");
			order.setListProduct(crabProductService.getProducts(couponId));
		}
		model.addAttribute("order", order);
		return "modules/crab/orderCreateForm";
	}
	
	@RequiresPermissions("crab:orderCreate:address")
	@RequestMapping(value = "getCountys")
	public String getCountys(Order order, Model model, RedirectAttributes redirectAttributes) {
		
		String province = order.getProvince();
		if(StringUtils.isNotBlank(province)){
			order.setListCity(crabAreaService.selectListCity(province));
		}
		String city = order.getCity();
		if(StringUtils.isNotBlank(city)){
			order.setListCounty(crabAreaService.selectListCounty(city));
		}
		String password  = order.getPassword();
		String pw = "";
		if(password!=null){
			pw = password.toString();
		}
		
		Map<String,Object> map =  couponDetailService.selCoupon(order.getCardNum(),pw);
		
		if(map!=null&&map.size()>0){
			String couponId = (String) map.get("couponId");
			order.setListProduct(crabProductService.getProducts(couponId));
		}
		model.addAttribute("order", order);
		return "modules/crab/orderCreateForm";
	}
	
	
	@RequiresPermissions("crab:orderCreate:getProducts")
	@RequestMapping(value = "getProducts")
	public String getProducts(Order order, Model model, RedirectAttributes redirectAttributes) {
		
		Object password  = order.getPassword();
		String pw = "";
		if(password!=null){
			pw = password.toString();
		}
		
		Map<String,Object> map =  couponDetailService.selCoupon(order.getCardNum(),"");
		
		if(map!=null&&map.size()>0){
			String passw = (String) map.get("password");
			if(pw.equals(passw)){
				String couponId = (String) map.get("couponId");
				order.setListProduct(crabProductService.getProducts(couponId));
				model.addAttribute("order", order);
			}else{
				model.addAttribute("message", "密码不正确");
			}
		}else{
			model.addAttribute("message", "兑换券卡号不正确或未激活");
		}
		
		return "modules/crab/orderCreateForm";
	}
	
}